package cn.qdhlm.frontendprocessor.common.helper;


import lombok.Data;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


/**
 * @Name:RocketMqProducer
 * @description:'
 * @author: xinY
 **/
@Component
public class RocketMqProducer {
    @Autowired
    private DefaultMQProducer producer;

    public void shutdown() {
        this.producer.shutdown();
    }

    public SendResult send(Message message) {
        try {
            SendResult sendResult = this.producer.send(message);
            return sendResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public SendResult send(String topic, byte[] data) {
        Message message = new Message(topic, data);
        try {
            return this.producer.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 发消息（topic,tags,data）
     *
     * @param topic
     * @param tags
     * @param data
     * @return
     */
    public SendResult send(String topic, String tags, byte[] data) {
        Message msg = new Message(topic, tags, data);
        return send(msg);
    }

    /**
     * 发消息（topic,tags,key,data）
     *
     * @param topic
     * @param tags
     * @param key
     * @param data
     * @return
     */
    public SendResult send(String topic, String tags, String key, byte[] data) {
        Message msg = new Message(topic, tags, key, data);
        return send(msg);
    }
}
